package Java网络编程.TCP通信程序练习.p3_服务器数据写入文本文件;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/*接收到的数据写入文件*/
public class ServerDemo {
    public static void main(String[] args) throws IOException {
        ServerSocket ss = new ServerSocket(2);

        Socket s = ss.accept();

        //接收数据---来自端口s的输入流
        BufferedReader br = new BufferedReader(new InputStreamReader(s.getInputStream()));
        //数据写入文本文件
        BufferedWriter bw = new BufferedWriter(new FileWriter("idea_test\\server.txt"));

        String line;
        while((line=br.readLine())!=null){
            if("886".equals(line)){
                break;
            }else{
                bw.write(line);
                bw.newLine();
                bw.flush();
            }
        }

        //释放资源需要额外把bw释放
        bw.close();
        ss.close();
    }
}
